package com.rljk.controller.app;

import com.rljk.common.consts.EnergyEnum;
import com.rljk.common.consts.IntegralEnum;
import com.rljk.common.consts.XWEnum;
import com.rljk.common.controller.BaseController;
import com.rljk.common.result.CodeMsg;
import com.rljk.common.result.Result;
import com.rljk.domain.common.PageData;
import com.rljk.redis.RedisService;
import com.rljk.service.UserManager;
import com.rljk.service.impl.AssetsService;
import com.rljk.service.impl.BriefIntroductionService;
import com.rljk.service.impl.WealthService;
import com.rljk.utils.AppUtil;
import com.rljk.utils.Tools;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.*;

/**
 * Author:   YASUO
 * Date:     2018/7/11 13:43
 * Description: 用户相关的财富接口
 */
@RestController
@RequestMapping("/wealth")
@Slf4j
public class WealthInController extends BaseController {

    @Resource(name = "wealthService")
    private WealthService wealthService;

    @Resource(name = "userService")
    private UserManager userService;

    @Autowired
    RedisService redisService;

    @Resource(name = "assetsService")
    private AssetsService assetsService;

    @Resource(name = "briefintroductionService")
    private BriefIntroductionService introductionService;


    /**
     * app获取财富（能量值，XW链，积分，幸运钻）
     * @Description
     * @Author zhaozhenhua
     * @date   2018年7月11日
     */
    @RequestMapping(value="/getvalue")
    //@Encrypt
    public Object getWealthValue() {
        log.info("获取财富（能量值，XW链，积分，幸运钻）");
        PageData pd = new PageData();
        pd = this.getPageData();
        log.info("请求参数："+pd.toString());
        Map<String,Object> map = new HashMap<>();
        try {
            if (Tools.checkKey("GETWEALTHVALUE", pd.getString("FKEY"))) {
                if (AppUtil.checkParam("addWealthValue",pd)){
                    //1.获取用户信息
                    String USER_ID = redisService.get(pd.getString("TOKEN"));
                    pd.put("USER_ID",USER_ID);
                    PageData user = userService.findById(pd);
                    if(user==null){
                        return Result.error(CodeMsg.EMPTY);
                    }

                    PageData asset = assetsService.findById(pd);

                    List<PageData> list = wealthService.getWealthValue(pd);
                    if(Tools.isEmptyList(list)){
                        return Result.error(CodeMsg.EMPTY);
                    }

                    String wealth_type = pd.getString("WEALTH_TYPE");
                    //获取简介
                    List<PageData> byType = introductionService.findByType("1");

                    switch (wealth_type){
                        case "1":  //能量
                            for (PageData p : list) {
                                log.info("GET_TYPE1:"+p.getString("GET_TYPE"));
                                log.info("GET_TYPE2:"+Integer.parseInt(p.getString("GET_TYPE")));
                                p.put("GET_TYPE", EnergyEnum.getName(Integer.parseInt(p.getString("GET_TYPE"))));
                            }
                            map.put("VALUE",asset.get("ENERGY_VALUE"));
                            map.put("TITLE",byType.get(1).getString("TITLE"));
                            map.put("INTRODUCTION",byType.get(1).getString("INTRODUCTION"));
                            break;
                        case "2":  //XW链
                            for (PageData p : list) {
                                p.put("GET_TYPE", XWEnum.getName(Integer.parseInt(p.getString("GET_TYPE"))));
                            }
                            map.put("VALUE",asset.get("XW_VALUE"));
                            map.put("TITLE",byType.get(0).getString("TITLE"));
                            map.put("INTRODUCTION",byType.get(0).getString("INTRODUCTION"));
                            break;
                        case "3":  //积分
                            for (PageData p : list) {
                                p.put("GET_TYPE", IntegralEnum.getName(Integer.parseInt(p.getString("GET_TYPE"))));
                            }
                            map.put("VALUE","");
                            map.put("TITLE","");
                            map.put("INTRODUCTION","");
                            break;
                        case "4":  //幸运钻
                            for (PageData p : list) {
                                p.put("GET_TYPE", EnergyEnum.getName(Integer.parseInt(p.getString("GET_TYPE"))));
                            }
                            map.put("VALUE",asset.get("LUCKY_DIAMOND"));
                            break;

                    }
                    map.put("list",list);
                    return Result.success(map);
                }else {
                    return Result.error(CodeMsg.PARAM_LACK);
                }
            } else {
                return Result.error(CodeMsg.FKEY_FAIL);
            }
        } catch (Exception e) {
            log.error(e.toString(), e);
            return Result.error(CodeMsg.SERVER_ERROR);
        }
    }

    /**
     * 获取宝石矿每天产生的宝石(每次返回10条数据)
     * author:YASUO
     * date:2018/7/31
     */
    @RequestMapping(value="/getxwstone")
    public Object getXWStone() {
        log.info("获取宝石矿每天产生的宝石(每次返回10条数据)");
        PageData pd = new PageData();
        pd = this.getPageData();
        log.info("请求参数："+pd.toString());
        Map<String,Object> map = new HashMap<>();
        try {
            if (Tools.checkKey("GETXWSTONE", pd.getString("FKEY"))) {
                String token = redisService.get(pd.getString("TOKEN"));
                if(Tools.isEmpty(token)){
                    return Result.error(CodeMsg.TOKEN_ERROR);
                }
                pd.put("USER_ID", token);
                ArrayList<PageData> list = new ArrayList();

//                Set<String> hkeys = redisService.hkeys(token);
//                for (String hkey : hkeys) {
//                    p.put("WEALTH_ID",hkey);
//                    p.put("WEALTH_VALUE",redisService.hget(token,hkey));
//                    list.add(p);
//                }

                //模糊查找
                Set<String> keys = redisService.keys(token+"_*");


                log.info("所有keys:"+keys);
                for (String key : keys) {
                    PageData p = new PageData();
                    log.info("key:"+key);
                    String[] split = key.split("_");
                    p.put("WEALTH_ID",split[1]);
                    p.put("WEALTH_VALUE",redisService.get(key));
                    if(list.size()<10){
                        list.add(p);
                    }else{
                        break;
                    }
                }

                PageData asset = assetsService.findById(pd);
                if(asset==null){
                    pd.put("ASSETS_ID",this.getSnowId());
                    assetsService.save(pd);
                }

                if (Tools.notEmptyList(list)) {
                    map.put("list",list);
                    map.put("XW_VALUE",asset.get("XW_VALUE"));
                    map.put("ENERGY_VALUE",asset.get("ENERGY_VALUE"));
                    return Result.success(map);
                } else {
                    return Result.error(CodeMsg.EMPTY);
                }
            } else {
                return Result.error(CodeMsg.FKEY_FAIL);
            }
        } catch (Exception e) {
            log.error(e.toString(), e);
            return Result.error(CodeMsg.SERVER_ERROR);
        }
    }

    /**
     * 宝石矿点击收取宝石
     * author:YASUO
     * date:2018/7/31
     */
    @RequestMapping(value="/clickxwstone")
    public Object clickXWStone() {
        log.info("宝石矿点击收取宝石");
        PageData pd = new PageData();
        pd = this.getPageData();
        log.info("请求参数："+pd.toString());
        try {
            if (Tools.checkKey("CLICKXWSTONE", pd.getString("FKEY"))) {
                if (AppUtil.checkParam("clickXWStone",pd)){
                    String token = redisService.get(pd.getString("TOKEN"));
                    pd.put("USER_ID",token);
                    String WEALTH_VALUE = redisService.get(token+"_"+pd.getString("WEALTH_ID"));
                    if(Tools.isEmpty(WEALTH_VALUE)){
                        return Result.error(CodeMsg.EMPTY);
                    }
                    Object xw = wealthService.clickXWStone(pd);
                    return Result.success(xw);
                }else {
                    return Result.error(CodeMsg.PARAM_LACK);
                }
            } else {
                return Result.error(CodeMsg.FKEY_FAIL);
            }
        } catch (Exception e) {
            log.error(e.toString(), e);
            return Result.error(CodeMsg.SERVER_ERROR);
        }
    }


    /**
     * 星我星球介绍和宝石总值，昨日总产值
     * author:YASUO
     * date:2018/8/15
     */
    @RequestMapping(value="/getgemstoneinto")
    public Object getGemstoneInto() {
        log.info("星我星球介绍和宝石总值，昨日总产值");
        PageData pd = new PageData();
        pd = this.getPageData();
        Map<String,Object>map = new HashMap<>();
        log.info("请求参数："+pd.toString());
        try {
            if (Tools.checkKey("GEMSTONEINTO", pd.getString("FKEY"))) {
                List<PageData> byType = introductionService.findByType("2");
                map.put("list",byType);
                map.put("total_value","18,784,525.00");
                map.put("yesterday_value","270,879.00");
                return Result.success(map);
            } else {
                return Result.error(CodeMsg.FKEY_FAIL);
            }
        } catch (Exception e) {
            log.error(e.toString(), e);
            return Result.error(CodeMsg.SERVER_ERROR);
        }
    }

}
